package uit.edu.vn.Activity;

//class music dung de load danh sach cac bai hat tu sdcard va dua vao list , 
//moi item cua list gom 1 imageview, 2 textview (ten bai hat , nhac sy),
//su dung thread de tach load nhac voi UI
import java.util.ArrayList;
import java.util.List;

import uit.edu.vn.Model.myMusic;
import uit.edu.vn.Utility.MusicListAdapter;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore.Audio;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import android.widget.ListView;

import android.widget.Toast;

public class MusicActivity extends ListActivity {
	private Cursor myCursor;
	private Handler handler;
	private ListView listview;
	private ProgressDialog loadingMusicDialog;
	private MusicListAdapter mAdapter ;
    private final List<myMusic> listMusic = new ArrayList<myMusic>();
    private ArrayList<Boolean> itemMusicChecked=new  ArrayList<Boolean>(); 
    private ArrayList<String> itemMusicString= new ArrayList<String>();
    private final int menu_Logout=0;
    private final int menu_Backup=1;
    private final int menu_MarkAll=2;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.backup_music);

		listview =getListView();
		listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);	  
		//goi dialog
		 loadingMusicDialog = ProgressDialog.show(MusicActivity.this, "","Đang Tải Dữ Liệu ...", true);
		// tao thread backgroundthread tach roi voi UI
		
		handler= new Handler();
	    Thread myMusicThread= new Thread(null,BackGroundThread,"MusicBackground");
		myMusicThread.start();
	
		 listview.setOnItemClickListener(new OnItemClickListener() 
	        {
	     	   @Override
	     	   public void onItemClick(AdapterView<?> arg0, View arg1, int position,
	 			long arg3) {
	     		   // TODO Auto-generated method stub
	     	       // int index = myCursor.getColumnIndexOrThrow(Audio.Media.DATA);
	     	       // myCursor.moveToPosition(position);
	     	       // String path=myCursor.getString(index);
	     	        int i= listview.getCheckedItemIds().length;
	     	        showToast(String.valueOf(i));        
	     	   }
	        });
    }
	private Runnable BackGroundThread = new Runnable() {
		
		@Override
		public void run() 
		{
			// TODO Auto-generated method stub
			//goi ham xu ly anh
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			doinBackGround();
	
		}
	};
	public void doinBackGround()
	{
	   //lấy cursor của music
        myCursor=this.managedQuery(android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
        startManagingCursor(myCursor);
        while (myCursor.moveToNext())
        {
            String musicName = myCursor.getString(myCursor.getColumnIndexOrThrow(Audio.Media.DISPLAY_NAME));
            String musicArtist= myCursor.getString(myCursor.getColumnIndexOrThrow(Audio.Media.ARTIST));
            listMusic.add(new myMusic(musicName, musicArtist));
        }
       // goij handle de update gui
        handler.post(doUpdate);
		
	}
	//ham xu ly thread
	public Runnable doUpdate= new Runnable() 
	{		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			 updateGUI();  
		}
	};
	public void updateGUI()
	{
		mAdapter= new MusicListAdapter(this,listMusic);
	    this.setListAdapter(mAdapter);

        // lay trang thai cac checkbox
        itemMusicChecked=mAdapter.getCheckList();
	    //xoa dialog
	     loadingMusicDialog.dismiss();
	}
	 //xu lý menu
   
	public void showToast(String message)
    {
   		Toast.makeText(this,message , Toast.LENGTH_SHORT).show();
    }
	 @Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    	// TODO Auto-generated method stub
	    	//return super.onCreateOptionsMenu(menu);
	    	 menu.add(3, menu_Logout, 3,"Đăng Xuất").setIcon(R.drawable.ic_error);
	    	 menu.add(1, menu_Backup, 2,"Sao Lưu").setIcon(R.drawable.ic_backup_select);
	    	 menu.add(2, menu_MarkAll,1, "Chon Hết").setIcon(R.drawable.ic_checkall);
			return true;
	    }
	    @Override
	    public boolean onOptionsItemSelected(MenuItem item) {
	    	// TODO Auto-generated method stub
	    	switch(item.getItemId())
	    	{
	    		case menu_Backup:
	    			//xu ly backup
	    			itemMusicString.clear();
	    			String path= null;
	    			for(int i=0;i<itemMusicChecked.size();i++)
	    			{
	    				// kiem tra mang item check , neu la
	    				//true thi lay vi tri va truyen vao 
	    				//cursor
	    				if(itemMusicChecked.get(i)==true)
	    				{
	    	    	        int index = myCursor.getColumnIndexOrThrow(Audio.Media.DATA);
	    	    	        myCursor.moveToPosition(i);
	    	    	        path=myCursor.getString(index);
	    	    	        //add vao mang itemString
	    					itemMusicString.add(path);
	    					Log.d("Chuoi", path); 
	    				}
	    			}
	    		case menu_Logout:
	    			//xu ly logout
	    			finish();
	    			return true;
	    		case menu_MarkAll:
	    			//xu ly check all
	    			for(int i=0;i<itemMusicChecked.size();i++)
	    		       {
	    				  itemMusicChecked.set(i, true);
	    		    	  mAdapter.setCheckList(itemMusicChecked);
	    		    	  
	    		       }
	    			this.setListAdapter(mAdapter);
	    			return true;
	    	}
	    	return false;
	    }
		
	
	}

